package token

import (
	"errors"
	"github.com/google/uuid"
	"time"
)

var (
	ErrExpiredToken = errors.New("token is expired")
	ErrInvalidToken = errors.New("token is invalid")
)

type Payload struct {
	ID        uuid.UUID
	Uid       string
	Ueid      string
	IssuedAt  time.Time `json:"issued_at"` // when the token is created
	ExpiredAt time.Time `json:"expired_at"`
	R3        int64
}

// NewPayload creates a new token payload with a specific username and duration
func NewPayload(uid string, ueid string, duration time.Duration, r3 int64) (*Payload, error) {
	tokenID, err := uuid.NewRandom()
	if err != nil {
		return nil, err
	}
	payload := &Payload{
		ID:        tokenID,
		Uid:       uid,
		Ueid:      ueid,
		IssuedAt:  time.Now(),
		ExpiredAt: time.Now().Add(duration),
		R3:        r3,
	}

	return payload, nil
}

func (payload *Payload) Valid() error {
	if time.Now().After(payload.ExpiredAt) {
		return ErrExpiredToken
	}
	return nil
}
